<?php
    include ("../lib/BLL/DmMonHocBLL.php");
    $thongbao="";
    $tt="";
    $ma="";
    $ten="";
    $tenvt="";
    $btnName="Thêm";
    $monhocBLL=new DmMonHocBLL();
    $dmmh=new DmMonHoc();
    $ds_monhoc=array();
    if (isset($_POST['timkiem'])) {
        $keysearch="";
		$where="";
        if(isset($_POST['txtkey'])){
    			$keysearch=$_POST['txtkey'];
    			$where="WHERE".$monhocBLL->buildSearchQueryLike($keysearch);
        }
    }
    
    if(!isset($_POST['timkiem'])||($_POST['txtkey']=="")){
        $where="WHERE 1=1";
    }
    
    if(isset($_GET['ma'])){
        $btnName="Sửa";
        $mamh=$_GET['ma'];
        $ds_monhoc=$monhocBLL->getItem($mamh);
        $ma=$ds_monhoc['madm_mh'];
        $ten=$ds_monhoc['ten_mh'];
        $tenvt=$ds_monhoc['ten_vt_mh'];
        $tt="readonly";
    }
    if(isset($_POST['btnLuu'])){
        $madm_mh=$_POST['mamonhoc'];
        $tenmh=$_POST['tenmonhoc'];
        $tenvtmh=$_POST['tenviettat'];
        $dmmh->setDmMonHoc($madm_mh,$tenmh,$tenvtmh);
        if($btnName=="Thêm"){
            $kq=$monhocBLL->Insertmonhoc($dmmh);
            if($kq==true){
                header("location: chuongtrinhdaotao-monhoc");
            }
        }else{
            $kq=$monhocBLL->updatemonhoc($dmmh);
            if ($kq == true){
                header("location: chuongtrinhdaotao-monhoc");
            }
        }
    }
    if(isset($_GET['xoama'])){
        $maxoa=$_GET['xoama'];
        $kq=$monhocBLL->deletedmmonhoc($maxoa);
        if ($kq == true){
            header("location: chuongtrinhdaotao-monhoc");
        }
    }
    if(isset($_POST['xoachon'])){
        if(isset($_POST['checkall'])){
            $kq=$monhocBLL->deleteAll();
            if ($kq == true){
                header("location: chuongtrinhdaotao-monhoc");
            }
        }
        if(isset($_POST['checkbox'])){
            $delete=$_POST['checkbox'];
            foreach($delete as $ds_monhoc)
            {
                $kq=$monhocBLL->deletedmmonhoc($ds_monhoc);
                if ($kq == true){
                    header("location: chuongtrinhdaotao-monhoc");
                }
            }
        }
    }
    

    //$ds=$monhocBLL->getListDmMonHoc();
    $ds=$monhocBLL->timkiem($where);
    $totalRecords=count($ds);           /// đếm số lượng bản gi
    $p=(isset($_GET['p']))?$_GET['p']:1;
    $limit=35;                           /// giởi hạn bản ghi đc hiện
    $offset=($p-1)*$limit; 
    $ds=$monhocBLL->getPages($where,$offset,$limit);
    $url="chuongtrinhdaotao-monhoc";
    $pages=$monhocBLL->paging($totalRecords,$limit,$url,'pagers');   /// gọi hàm phân trang
    
    //////////////////////////////////////////////////
    $smarty->assign('ds_mh',$ds);
    $smarty->assign('btnName',$btnName);
    $smarty->assign('p',$p);
    $smarty->assign('pages',$pages);
    $smarty->assign('ma',$ma);
    $smarty->assign('ten',$ten);
    $smarty->assign('tenvt',$tenvt);
    $smarty->assign('tt',$tt);
    $smarty->assign('thongbao',$thongbao);
    $smarty->display('chuongtrinhdaotao/dmMonHoc.tpl');
?>